package com.cloudcc.mobile.parser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.cloudcc.mobile.entity.ContactEntity;
import com.cloudcc.mobile.entity.ContactMessage;
import com.cloudcc.mobile.util.Json;
import com.cloudcc.mobile.util.Tools;

public class ContactParser {
	/**
	 * 解析查询的客户
	 * @param jsonString
	 * @return
	 */
	public  static ContactMessage getContacts(String jsonString){
		ArrayList<ContactEntity> list=new ArrayList<ContactEntity>();
		ContactMessage message=new ContactMessage();
		try {
			JSONObject obj=new JSONObject(jsonString);
			message.setResult(obj.getBoolean("result"));
			message.setReturnInfo(obj.getString("returnInfo"));
			message.setReturnCode(obj.getString("returnCode"));
			JSONArray json=obj.getJSONArray("data");
			for (int i = 0; i < json.length(); i++) {
				JSONObject jsonObj=json.getJSONObject(i);
				ContactEntity contact=(ContactEntity) Json.toObject(jsonObj.toString(),ContactEntity.class);
				list.add(contact);
			}
			message.setList(list);
		} catch (JSONException e) {
			Tools.handle(e);
		}
		return message;
	}
	
	/**
	 * 获取所有客户
	 * 
	 */
	
	public static Map<String,ContactEntity> getAllCallRecords(Context context,String jsonString) {
		ContactMessage contactMessage=getContacts(jsonString);
		if(!contactMessage.isResult()){
			return null;
		}
		List<ContactEntity> contact=contactMessage.getList();
		Map<String,ContactEntity> map=new HashMap<String, ContactEntity>();
		if(contact!=null){
			for (int i = 0; i < contact.size(); i++) {
				String name=contact.get(i).getName();
				map.put(name, contact.get(i));
			}
			return map;
		}
		return null;
	}
	
}
